#include<bits/stdc++.h>
using namespace std;
#define N 2010
const int p=1e9+7;
map<long long,int>mp;
int n,a[N],b[N],f[N],ans;
int jc[N],inv[N],finv[N];
long long hx;

void dfs(int x,int y)
{
	if(x==n&&y==n)
	{
		hx=0;
		for(int i=1;i<=n*2-2;++i)hx=hx*10+f[i];
		if(!mp[hx])
		{
			mp[hx]=1;++ans;
		}
		return;
	}
	if(x!=n){f[x+y-1]=b[y];dfs(x+1,y);}
	if(y!=n){f[x+y-1]=a[x];dfs(x,y+1);}
}

int main()
{
	freopen("zha.in","r",stdin);
	freopen("zha.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;++i)scanf("%d",&a[i]);
	for(int i=1;i<=n;++i)scanf("%d",&b[i]);
	if(n<=10){dfs(1,1);printf("%d",ans);}
	else
	{
		jc[0]=jc[1]=inv[1]=finv[0]=finv[1]=1;
		for(int i=2;i<=n+n;++i)
		{
			jc[i]=1ll*jc[i-1]*i%p;
			inv[i]=1ll*(p-p/i)*inv[p%i]%p;
			finv[i]=1ll*finv[i-1]*inv[i]%p;
		}
		ans=1ll*jc[n*2-2]*finv[n-1]%p*finv[n-1]%p;
		if(a[1]==b[1])ans=1ll*ans*inv[n]%p;
		printf("%d",ans);
	}
	return 0;
}
